package de.cbf.cam.dsa.person;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import de.cbf.cam.dsa.person.base.ControllerPersonBaseDSA;
import de.cbf.cam.dsa.person.entities.PersonBaseDSA;
import de.cbf.cam.dsa.person.extension.PersonExtensionPointResovler;
import de.cbf.cam.dsa.person.views.InitializeControllersListener;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSAProviderMonths;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSARCPFinder;

/**
 * The activator class controls the plug-in life cycle
 * TODO create extension points for generators
 */
public class ActivatorPersonDSA extends AbstractUIPlugin {
	private static final Log logger = LogFactory.getLog(ActivatorPersonDSA.class);

	// The plug-in ID
	public static final String PLUGIN_ID = "de.cbf.cam.dsa.person"; //$NON-NLS-1$

	// The shared instance
	private static ActivatorPersonDSA plugin;

	//entities to share
	protected PersonBaseDSA 			activePerson;

	protected IServiceDSAProviderMonths	providerMonths;
	protected IServiceDSARCPFinder	 	rcpFinder;



	protected InitializeControllersListener initConrollerListener;

	protected ControllerPersonBaseDSA controllerPersonBase;

	/**
	 * The constructor
	 */
	public ActivatorPersonDSA() {
		initConrollerListener = new InitializeControllersListener();
	}



	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		findProviderServices(context);
		//make special port for extension points
		PersonExtensionPointResovler.getInstance().setBundleContext(context);
	}

	protected void findProviderServices(BundleContext context) {
		logger.debug("trying to find provider services...");
		findProviderMonths(context);
		findProviderRCPStructure(context);
		logger.debug("provider services aquired.");
	}
	protected void findProviderMonths(BundleContext context) {
		ServiceReference<IServiceDSAProviderMonths> serviceProviderMonthsRef = context.getServiceReference(IServiceDSAProviderMonths.class);
	    if (serviceProviderMonthsRef != null){
	    	setProviderMonths(context.getService(serviceProviderMonthsRef));
	    }
	}

	protected void findProviderRCPStructure(BundleContext context) {
		ServiceReference<IServiceDSARCPFinder> serviceRCPFinderRef = context.getServiceReference(IServiceDSARCPFinder.class);
	    if (serviceRCPFinderRef != null){
	    	setRCPFinder(context.getService(serviceRCPFinderRef));
	    }
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}


	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static ActivatorPersonDSA getDefault() {
		return plugin;
	}



	/**
	 * method to register listeners for opening views at the workbench<br>
	 * only call after workbench was created
	 */
	public void registerViewOpenListeners(){
		IWorkbench workbench = getWorkbench();
		IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
		IPartService partService =  workbenchWindow.getPartService();
		partService.addPartListener(initConrollerListener);
	}

	/**
	 * @return
	 */
	public PersonBaseDSA getActivePerson() {
		return activePerson;
	}

	/**
	 * @return
	 */
	public IServiceDSAProviderMonths getProviderMonths() {
		return providerMonths;
	}

	/**
	 * @param providerMonths the providerMonths to set
	 */
	public void setProviderMonths(IServiceDSAProviderMonths providerMonths) {
		this.providerMonths = providerMonths;
		if(this.providerMonths == null){
			logger.error("no provider months found !");
			return;
		}
		logger.debug("provider months set to: "+this.providerMonths.getName());
	}


	/**
	 * @return
	 */
	public IServiceDSARCPFinder getRCPFinder() {
		return rcpFinder;
	}

	/**
	 * @param rcpFinder the rcpFinder to set
	 */
	public void setRCPFinder(IServiceDSARCPFinder rcpFinder) {
		this.rcpFinder = rcpFinder;
		if(this.rcpFinder == null){
			logger.error("no rcp finder provided!");
			return;
		}
		logger.debug("provider rcp finder set to: "+this.rcpFinder.getName());
	}

	/**
	 * @param activePerson the activePerson to set
	 */
	public void setActivePerson(PersonBaseDSA activePerson) {
		this.activePerson = activePerson;
	}

	/**
	 * @return
	 */
	public ControllerPersonBaseDSA getControllerPersonBase() {
		return controllerPersonBase;
	}

	/**
	 * @param controllerPersonBase the controllerPersonBase to set
	 */
	public void setControllerPersonBase(ControllerPersonBaseDSA controllerPersonBase) {
		this.controllerPersonBase = controllerPersonBase;
	}


}
